package logical_spaces.beans;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author juan
 */
 
import Entity.TableSpaceInfo;
import java.io.Serializable;  
import java.sql.Connection;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import logical_spaces.business.GraphicsLogicalSpaceBusiness;
import logical_spaces.business.TableSpaceInfoDataModel;
import login.beans.ConectionBean;
import org.primefaces.model.chart.CartesianChartModel;  
import org.primefaces.model.chart.ChartSeries;  


@ManagedBean
@RequestScoped
public class ReviewTableSpaceStateBean implements Serializable {  
  
    private List<TableSpaceInfo> tableSpacesInfo;
    private TableSpaceInfoDataModel mediumTableSpaceInfoModel;
    private static final long serialVersionUID = 308L;
    private TableSpaceInfo selectedTableSpaceInfo;   
    private Connection conn;
    
    private CartesianChartModel categoryModel;

    public ReviewTableSpaceStateBean() {
        
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");        

        this.conn = conectionBean.currentConnection;
        
        createCategoryModel();
        
        mediumTableSpaceInfoModel = new TableSpaceInfoDataModel(tableSpacesInfo);
        
    }     

    public List<TableSpaceInfo> getTableSpacesInfo() {
        return tableSpacesInfo;
    }

    public void setTableSpacesInfo(List<TableSpaceInfo> tableSpacesInfo) {
        this.tableSpacesInfo = tableSpacesInfo;
    }

    public TableSpaceInfoDataModel getMediumTableSpaceInfoModel() {
        return mediumTableSpaceInfoModel;
    }

    public void setMediumTableSpaceInfoModel(TableSpaceInfoDataModel mediumTableSpaceInfoModel) {
        this.mediumTableSpaceInfoModel = mediumTableSpaceInfoModel;
    }

    public TableSpaceInfo getSelectedTableSpaceInfo() {
        return selectedTableSpaceInfo;
    }

    public void setSelectedTableSpaceInfo(TableSpaceInfo selectedTableSpaceInfo) {
        this.selectedTableSpaceInfo = selectedTableSpaceInfo;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public void setCategoryModel(CartesianChartModel categoryModel) {
        this.categoryModel = categoryModel;
    }

    private void createCategoryModel() { 
        
        categoryModel = new CartesianChartModel();  
        GraphicsLogicalSpaceBusiness graphicsLogicalSpaceBusiness 
                = new GraphicsLogicalSpaceBusiness(conn);
        
        ChartSeries libre = new ChartSeries();  
        ChartSeries usado = new ChartSeries();  
        libre.setLabel("Libre");  
        usado.setLabel("Usado");  
        tableSpacesInfo = graphicsLogicalSpaceBusiness.info();
        
        for (TableSpaceInfo tablespaceInfo :  tableSpacesInfo) {
            libre.set(tablespaceInfo.getNombre(), tablespaceInfo.getEspacioLibre());     
            usado.set(tablespaceInfo.getNombre(), tablespaceInfo.getEspacioUsado()); 
            
        } 
        categoryModel.addSeries(usado); 
        categoryModel.addSeries(libre);
         
    }  
    
}  
 
